/* Copyright (C) 2012  	Mathieu Marleix <mathieu.marleix@aerian.fr>
 *						Doris Vaton <doris.vaton@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shaipicku.mjpegcams;

import java.util.Locale;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

/**
 * Display the settings of the applications
 * @author Mathieu Marleix <mathieu.marleix@aerian.fr>
 * @author Doris Vaton <doris.vaton@gmail.com>
 *
 */
public class SettingsActivity extends PreferenceActivity{

	LayoutParams params = null;
	private SharedPreferences sharedPrefs = null;
	public Locale locale = null;
	private static int CAMERA_NUMBER = 2;
	private OnSharedPreferenceChangeListener listener = null;
	private Boolean DEBUG = false;
	public static final String TAG="SETTINGS";

	@Override
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		
		Bundle param = this.getIntent().getExtras();
		this.DEBUG = (Boolean) param.getBoolean("DEBUG",false);
		sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

		Configuration config = getBaseContext().getResources().getConfiguration();
		String lang = sharedPrefs.getString("languages", "");
		if(DEBUG) Log.d(TAG, sharedPrefs.getString("languages", ""));
		if (!"".equals(lang) &&! config.locale.getLanguage().equals(lang))
		{
			locale = new Locale(lang);
			Locale.setDefault(locale);
			config.locale = locale;
			getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
		}
		setContentView(R.layout.setting);
		

		String value = sharedPrefs.getString("cam_number", null);
	    if(value == null)
	    	CAMERA_NUMBER = 2;
	    else {
	    	try { 
	    		if( (CAMERA_NUMBER = Integer.valueOf(value)) > 10) throw new NumberFormatException();
	    	}catch (NumberFormatException e) {
				// TODO: handle exception
	    		Toast.makeText(SettingsActivity.this, getString(R.string.cam_bad_number), Toast.LENGTH_SHORT).show();
	    		CAMERA_NUMBER = 2;
			}
	    	
	    }

		addPreferencesFromResource(R.xml.preferences);
	    for(int i = 0 ; i < CAMERA_NUMBER ; ){
	    	EditTextPreference etp = new EditTextPreference(this);
	    	etp.setDefaultValue("");
	    	etp.setKey("cam_url"+i);
	    	i++;
	    	etp.setDialogTitle(getString(R.string.cam_url) + " " + i);
	    	etp.setTitle(getString(R.string.cam_url) + " " + i);
	    	etp.setSummary(getString(R.string.cam_url_summary));
	    	PreferenceScreen ps = getPreferenceScreen();
	    	ps.addItemFromInflater(etp);
	    }

		//When there is a modification we relaunch the activity.
		listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
			public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
				if(key.equals("languages")){
					startActivity(getIntent());
					SettingsActivity.this.finish();
				}

				else if (key.equals("cam_number")){
					startActivity(getIntent());
					SettingsActivity.this.finish();
				}
			}
		};

		sharedPrefs.registerOnSharedPreferenceChangeListener(listener);
		loadSetting();
	}
	public void loadSetting(){
		Button about = (Button) findViewById(R.id.about);
		about.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SettingsActivity.this,StartActivity.class);
				startActivity(intent);
				finish();
			}
		});
		
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		// refresh the tabs and their content
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menusetting, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		//Get the item selected
		switch (item.getItemId()) {
		case R.id.main:
			finish();
			return true;
		}
		return false;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			Intent intent = new Intent(SettingsActivity.this,StartActivity.class);
			startActivity(intent);
			finish();
		}return false;
	}
	
	/**
	 * @return the dEBUG
	 */
	public Boolean getDEBUG() {
		return DEBUG;
	}
	/**
	 * @param dEBUG the dEBUG to set
	 */
	public void setDEBUG(Boolean dEBUG) {
		DEBUG = dEBUG;
	}
}